#include "lcd_init.h"
#include "stdio.h"
#include "app.h"

void wr_cmd_parall1_8(uint16_t par)
{ 
	LCD_DCXLow();
	LCD_CSLow();
	LCD_WRLow();
	GPIOB->ODR=par;
	
	LCD_WRHigh();
}
void wr_dat_parall1_8(uint16_t par)
{ 
	LCD_DCXHigh();
	LCD_WRLow();
	GPIOB->ODR=par;
	
	LCD_WRHigh();
}
void wr_displ_parall1_8(uint8_t color)
{ 
	LCD_DCXHigh();
	LCD_WRLow();
	GPIOB->ODR=color;
	LCD_WRHigh();
}
void S240DLBH_Init(void)
{
    /*LCD_RSTHigh();
    HAL_Delay(120);
    LCD_RSTLow();
    HAL_Delay(120);
    LCD_RSTHigh();
    HAL_Delay(200);*/              
	/****************并口1-8bit*****************/
	//LCD_SCLLow();
	//HAL_Delay(5);
	//LCD_SCLHigh(); 
	LCD_CSLow();//CS=0(Ñ¡ÖÐ)
	wr_cmd_parall1_8(0x11); // Exit Sleep
	HAL_Delay(120);

	wr_cmd_parall1_8(0x36);
	wr_dat_parall1_8(0x00); // 00

	wr_cmd_parall1_8(0x3A);
	wr_dat_parall1_8(0x06); // 06

	/*wr_cmd_parall1_8(0xB2);
	wr_dat_parall1_8(0x0C);//0c
	wr_dat_parall1_8(0x0C);
	wr_dat_parall1_8(0x00);
	wr_dat_parall1_8(0x33);//33
	wr_dat_parall1_8(0x33);*/

	wr_cmd_parall1_8(0xB7);
	wr_dat_parall1_8(0x35);

	wr_cmd_parall1_8(0xBB);
	wr_dat_parall1_8(0x32);

	wr_cmd_parall1_8(0xC0);
	wr_dat_parall1_8(0x2C);

	wr_cmd_parall1_8(0xC2);
	wr_dat_parall1_8(0x01);

	wr_cmd_parall1_8(0xC3);
	wr_dat_parall1_8(0x18);

	wr_cmd_parall1_8(0xC4);
	wr_dat_parall1_8(0x20);

	wr_cmd_parall1_8(0xC6);
	wr_dat_parall1_8(0x0F);

	wr_cmd_parall1_8(0xCC);
	wr_dat_parall1_8(0x00);

	wr_cmd_parall1_8(0xD0);
	wr_dat_parall1_8(0xA4);
	wr_dat_parall1_8(0xA1);

	wr_cmd_parall1_8(0xE0); // Set Gamma
	wr_dat_parall1_8(0xF0);
	wr_dat_parall1_8(0x03);
	wr_dat_parall1_8(0x0C);
	wr_dat_parall1_8(0x0C);
	wr_dat_parall1_8(0x11);
	wr_dat_parall1_8(0x0A);
	wr_dat_parall1_8(0x45);
	wr_dat_parall1_8(0x00);
	wr_dat_parall1_8(0x45);
	wr_dat_parall1_8(0x2A);
	wr_dat_parall1_8(0x13);
	wr_dat_parall1_8(0x11);
	wr_dat_parall1_8(0x25);
	wr_dat_parall1_8(0x25);

	wr_cmd_parall1_8(0xE1); // Set Gamma
	wr_dat_parall1_8(0xF0);
	wr_dat_parall1_8(0x03);
	wr_dat_parall1_8(0x0A);
	wr_dat_parall1_8(0x17);
	wr_dat_parall1_8(0x12);
	wr_dat_parall1_8(0x09);
	wr_dat_parall1_8(0x28);
	wr_dat_parall1_8(0x55);
	wr_dat_parall1_8(0x4F);
	wr_dat_parall1_8(0x09);
	wr_dat_parall1_8(0x15);
	wr_dat_parall1_8(0x17);
	wr_dat_parall1_8(0x17);
	wr_dat_parall1_8(0x1E);

	//wr_cmd_parall1_8(0x35);
	//wr_dat_parall1_8(0x00);
	HAL_Delay(200);

	//wr_cmd_parall1_8(0x51);
	//wr_dat_parall1_8(0x88);

	wr_cmd_parall1_8(0x29); // Display on

	HAL_Delay(200);
}
//void Filllcd_Area(uint16_t _usX, uint16_t _usY, uint16_t _usWidth , uint16_t _usHeight, uint16_t color)
void Filllcd_Area(uint32_t color)
{
    uint32_t x = 0; uint32_t y = 0;
    wr_cmd_parall1_8(0x2a);
    wr_dat_parall1_8(0x00);
    wr_dat_parall1_8(0x0a);
    wr_dat_parall1_8(0x00);
    wr_dat_parall1_8(0xef);

    wr_cmd_parall1_8(0x2b);
    wr_dat_parall1_8(0x00);
    wr_dat_parall1_8(0x0a);
    wr_dat_parall1_8(0x01);
    wr_dat_parall1_8(0x3F);

	wr_cmd_parall1_8(0X2C);
	uint8_t colorred = color >> 16;
    uint8_t color1 = (color & 0xff00) >> 8;
    uint8_t color2 = (color & 0xff);
	for( y=0; y < LCD_Height ; y++ )
		{for( x=0; x < LCD_Width; x++ )
		{
			wr_displ_parall1_8(colorred);
			wr_displ_parall1_8(color1);
			wr_displ_parall1_8(color2);	
			//wr_displ_parall2_18_H(0x0003);	
			//wr_displ_parall2_18_L(0xF000);            
		}
	}
}